package spider;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import jxl.write.WriteException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class CsmbriUtil2{
	
	public static void doIt(){
		getFile(1);
		getFile(101);
		getFile(201);
		getFile(301);
		getFile(401);
		getFile(501);
		getFile(601);
		getFile(701);
		getFile(801);
		getFile(901);
		getFile(1001);
		getFile(1101);
		getFile(1201);
		getFile(1301);
		getFile(1401);
		getFile(1501);
		getFile(1601);
		getFile(1701);
	}
	
	
	//http://www.csmbri.com/data/ifo/t2.htm
	
	public static void getFile(int pageIndex) {
		
		String fileName =pageIndex+".xlsx";
		Long s = null;
		Long e = null;
		int maxPage = pageIndex + 100;
			for(int page=pageIndex;page<maxPage;page++){
				try {
					s = new Date().getTime();
					System.out.println("正在拉取第："+page+"页数据，还剩："+(maxPage-page-1)+"页数据");
					JSONObject pageData = getData(page);
//					writeData(pageData,"c://file//"+fileName);
					writeData(pageData,File.separator+"root"+File.separator+"file"+File.separator+fileName);
					e = new Date().getTime();
					System.out.println("写入第一页成功,耗时："+(e-s)/1000+"秒");
				} catch (Exception ee) {
					ee.printStackTrace();
					System.out.println("写入第页"+page+"失败："+ee.getMessage());
				}
			}
	}
	
	public static JSONObject getData(int page){
		String url = "http://www.csmbri.com/data/ifo/t2/json?_search=false&nd=1481506007863&rows=300&page="+page+"&sord=asc";
		JSONObject  ret = CommonUtil.httpRequest(url, "POST", "");
//		System.out.println(ret.toString());
		
		
		
		return ret;
	}
	
	public static void writeData(JSONObject ret,String fileName) throws WriteException, FileNotFoundException, Exception{
		
		
		String[] title = new String[]{"填报年月","账号*","名称","市","区（县、市）*","乡镇 （街道）*","其他说明*","企业负责人*","填表负责人*","填表人*","联系电话*","手机*","填报日期",
				"工业总产值-本月","工业总产值-本月止累计","工业总产值-去年本月","工业总产值-去年本月止累计","工业总产值-累计同比增长",
				"新产品产值-本月","新产品产值-本月止累计","新产品产值-去年本月","新产品产值-去年本月止累计","新产品产值-累计同比增长",
				"工业销售产值-本月","工业销售产值-本月止累计","工业销售产值-去年本月","工业销售产值-去年本月止累计","工业销售产值-累计同比增长",
				"出口交货值-本月","出口交货值-本月止累计","出口交货值-去年本月","出口交货值-去年本月止累计","出口交货值-累计同比增长",
				"工业生产电力消费-本月","工业生产电力消费-本月止累计","工业生产电力消费-去年本月","工业生产电力消费-去年本月止累计","工业生产电力消费-累计同比增长",
				"电子商务销售额-本月","电子商务销售额-本月止累计","电子商务销售额-去年本月","电子商务销售额-去年本月止累计","电子商务销售额-累计同比增长"
				};
		
		
		if("0".equals(ret.getString("code")))
		{
			JSONArray rows = ret.getJSONArray("rows");
			List<Object[]> list = new ArrayList<Object[]>();
			for(int i=0;i<rows.size();i++)
			{
				JSONObject row = rows.getJSONObject(i);
				Object[] data = new Object[43];
				data[0] = row.getString("bdate").substring(0, 10);
				data[1] = trans(row.getString("f001"));//*
				data[2] = row.getString("f002");
				data[3] = row.getString("f003");//市
				
				data[4] = row.getString("f004");//*
				data[5] = row.getString("f005");//*
				data[6] = row.getString("f006");//*
				data[7] = row.getString("f007");//*
				data[8] = row.getString("f008");//*填表负责人
				data[9] = row.getString("f009");//*
				data[10] = trans(row.getString("f010"));//*联系电话
				data[11] = trans(row.getString("f011"));//*手机
				data[12] = row.getString("f013");//*
				
				data[13] = row.getString("f012");//1
				data[14] = row.getString("f014");
				data[15] = row.getString("f015");
				data[16] = row.getString("f016");
				data[17] = row.getString("f017");
				
				data[18] = row.getString("f018");//2
				data[19] = row.getString("f019");
				data[20] = row.getString("f020");
				data[21] = row.getString("f021");
				data[22] = row.getString("f022");
				
				data[23] = row.getString("f023");//3
				data[24] = row.getString("f024");
				data[25] = row.getString("f025");
				data[26] = row.getString("f026");
				data[27] = row.getString("f027");
				
				data[28] = row.getString("f028");//4
				data[29] = row.getString("f029");
				data[30] = row.getString("f030");
				data[31] = row.getString("f031");
				data[32] = row.getString("f032");
				
				data[33] = row.getString("f033");//5
				data[34] = row.getString("f034");
				data[35] = row.getString("f035");
				data[36] = row.getString("f036");
				data[37] = row.getString("f037");
				
				data[38] = row.getString("f038");//6
				data[39] = row.getString("f039");
				data[40] = row.getString("f040");
				data[41] = row.getString("f041");
				data[42] = row.getString("f042");
				
				list.add(data);
			}
			File f= new File(fileName);
			//写入Excel
			ExcelUtil.createExcel(f, 
					list, title, "工业产销监测数据", new String[]{"30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							"30","30","30","30","30",
							},f.exists());
		}else
		{
			System.out.println("拉取数据异常");
		}
		
		
		
	}
	
	public static String trans(String str){
		try {
			BigDecimal db = new BigDecimal(str);
			return db.toString();
		} catch (Exception e) {
			return str;
		}
	}

}
